#ifndef AST_H
#define AST_H

#include "token.h"

// AST节点类型
typedef enum {
    NODE_CREATE_TABLE,
    NODE_INSERT,
    NODE_SELECT,
    NODE_DELETE,
    NODE_DROP_TABLE,
    NODE_ALTER_TABLE,
    NODE_UPDATE,
    NODE_COLUMN_DEFINITION,
    NODE_EXPRESSION,
    NODE_VALUE,
    NODE_IDENTIFIER,
    NODE_BINARY_OP
} ASTNodeType;

// ALTER TABLE操作类型枚举
typedef enum {
    ALTER_ADD_COLUMN,
    ALTER_DROP_COLUMN,
    ALTER_MODIFY_COLUMN
} AlterActionType;

// 表达式类型
typedef enum {
    EXPR_IDENTIFIER,
    EXPR_NUMBER,
    EXPR_STRING,
    EXPR_BINARY_OP
} ExprType;

// 列数据类型
typedef enum {
    TYPE_INT,
    TYPE_VARCHAR,
    TYPE_FLOAT,
    TYPE_DOUBLE
} ColumnType;

// 抽象语法树节点结构
typedef struct ASTNode {
    ASTNodeType type;
    struct ASTNode* left;
    struct ASTNode* right;
    Token token;
    void* extra_data;
} ASTNode;

// 列定义结构
typedef struct {
    char* name;
    ColumnType type;
    int length;
} ColumnDefinition;

// CREATE TABLE语句结构
typedef struct {
    char* table_name;
    ColumnDefinition* columns;
    int column_count;
} CreateTableStmt;

// INSERT语句结构
typedef struct {
    char* table_name;
    char** columns;
    int column_count;
    ASTNode** values;
    int value_count;
} InsertStmt;

// SELECT语句结构
typedef struct {
    ASTNode** columns;
    int column_count;
    char* table_name;
    ASTNode* where_clause;
} SelectStmt;

// DELETE语句结构
typedef struct {
    char* table_name;
    ASTNode* where_clause;
} DeleteStmt;

// DROP TABLE语句结构
typedef struct {
    char* table_name;
    int if_exists;  // 1表示IF EXISTS存在，0表示不存在
} DropTableStmt;

// ALTER TABLE语句结构
typedef struct {
    char* table_name;
    AlterActionType action;   // 修改为枚举类型
    char* column_name;  // 涉及的列名
    ColumnType column_type;  // 列类型
    int column_length;  // 列长度
} AlterTableStmt;

// UPDATE语句结构
typedef struct {
    char* table_name;
    char** column_names;
    ASTNode** values;
    int set_count;
    ASTNode* where_clause;
} UpdateStmt;

// 表达式结构
typedef struct {
    ExprType type;
    union {
        char* identifier;
        char* string_value;
        double number_value;
        struct {
            ASTNode* left;
            ASTNode* right;
            Token op;
        } binary_op;
    } value;
} Expression;

ASTNode* create_ast_node(ASTNodeType type, Token token);
void free_ast_node(ASTNode* node);

#endif // AST_H